<?php

require_once(DIR_LIB."vslibdef.php");

if(!defined('DIR_LIB_VSCORE'))
	define('DIR_LIB_VSCORE', DIR_LIB."VsCore".DIR_SEP);

if(!defined('DIR_LIB_VSSHOP'))
	define('DIR_LIB_VSSHOP', DIR_LIB."VsShop".DIR_SEP);

require_once(DIR_LIB_VSCORE."vsapplication.php");

require_once(DIR_LIB_VSSHOP."vsproduct.php");

class VsProductFactory {
	private static $_Instance = NULL;

	private $_app = NULL;

	var $_sqlTable='products';

	private $_pager = NULL;

	static function Instance() {
		if(!self::$_Instance)
			self::$_Instance = &new VsProductFactory();

		return self::$_Instance;
	}
	//===============================================================================

	function setPager(&$pager) { $this->_pager = $pager; }
	//==============================================================================

	function searchProducts($search, $key='name') {
		if($this->_app->isMultiLang())
			return $this->searchProducts_MultiLang($search, $key);
		else
			return $this->searchProducts_SingleLang($search, $key);
	}
	//===============================================================================

	function makeProducts($catId=0) {
		if($this->_app->isMultiLang())
			return $this->makeProducts_MultiLang($catId);
		else
			return $this->makeProducts_SingleLang($catId);
	}
	//===============================================================================

	function makeTopProducts($catId=0) {
		if($this->_app->isMultiLang())
			return $this->makeTopProducts_MultiLang($catId);
		else
			return $this->makeTopProducts_SingleLang($catId);
	}
	//===============================================================================

	function makeProduct($prdId) {
		if($this->_app->isMultiLang())
			return $this->makeProduct_MultiLang($prdId);
		else
			return $this->makeProduct_SingleLang($prdId);
	}
	//===============================================================================

	private function __construct() {
		$this->_app = VsApplication::Instance();

	}
	//==============================================================================

	private function install()  {

	}
	//==============================================================================

	private function searchProducts_MultiLang($search, $key='name') {
		$db = $this->_app->db();
		$listProducts = array();

		$q = "SELECT * FROM `products` WHERE ";
		if($key=='name')
			$q .= "name LIKE '".$search."'";
		else
			$q .= $key."=".$search;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		//echo $q;

		$res = $db->query($q);
		while($rPrd=@mysql_fetch_object($res)) {
			$prd = new VsProduct();

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->products_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;
			

			array_push($listProducts, $prd);
		}

		//echo "Products Count: ". sizeof($listProducts);

		return $listProducts;
	}
	//===============================================================================

	private function makeProducts_MultiLang($catId=0) {
		$db = $this->_app->db();
		$listProducts = array();

		$q = "SELECT * FROM `products`";
		if($catId)
			$q .= " WHERE products_categories_id=".$catId;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		//echo $q;

		$res = $db->query($q);
		while($rPrd=@mysql_fetch_object($res)) {
			$prd = new VsProduct();

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->products_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;

			$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			$prd->_image = isset($rPrd->image) ? $rPrd->image : '';
			

			array_push($listProducts, $prd);
		}

		//echo "Products Count: ". sizeof($listProducts);

		return $listProducts;
	}
	//===============================================================================

	private function makeTopProducts_MultiLang($catId=0) {
		$db = $this->_app->db();
		$listProducts = array();

		$q = "SELECT * FROM `products`";
		if($catId)
			$q .= " WHERE products_categories_id=".$catId;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		//echo $q;

		$res = $db->query($q);
		while($rPrd=@mysql_fetch_object($res)) {
			$prd = new VsProduct();

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->product_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;

			$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			$prd->_image = isset($rPrd->image) ? $rPrd->image : '';
			

			array_push($listProducts, $prd);
		}

		//echo "Products Count: ". sizeof($listProducts);

		return $listProducts;
	}
	//===============================================================================

	private function makeProduct_MultiLang($prdId) {
		$db = $this->_app->db();
		$prd = new VsProduct();

		$q = "SELECT * FROM `products` WHERE id=".$prdId;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		$res = $db->query($q);
		if($res) {
			$rPrd=@mysql_fetch_object($res);

			$prd->_id = $rPrd->id;
			$prd->_categoryId = @$rPrd->product_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;

			//$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			//$prd->_image = isset($rPrd->image) ? $rPrd->image : '';
		}

		return $prd;
	}
	//===============================================================================

	private function searchProducts_SingleLang($search, $key='name') {
		$db = $this->_app->db();
		$listProducts = array();

		$q = "SELECT * FROM `products` WHERE ";
		if($key=='name')
			$q .= "name LIKE ".$search;
		else
			$q .= $key."=".$search;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		//echo $q;

		$res = $db->query($q);
		while($rPrd=@mysql_fetch_object($res)) {
			$prd = new VsProduct();

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->products_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;

			$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			$prd->_image = isset($rPrd->image) ? $rPrd->image : '';
			

			array_push($listProducts, $prd);
		}

		//echo "Products Count: ". sizeof($listProducts);

		return $listProducts;
	}
	//===============================================================================

	private function makeProducts_SingleLang($catId=0) {
		$db = $this->_app->db();
		$listProducts = array();

		$q = "SELECT * FROM `products`";
		if($catId)
			$q .= " WHERE products_categories_id=".$catId;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		//echo $q;

		$res = $db->query($q);
		while($rPrd=@mysql_fetch_object($res)) {
			$prd = new VsProduct();

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->product_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;
			

			$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			$prd->_image = isset($rPrd->image) ? $rPrd->image : '';

			array_push($listProducts, $prd);
		}

		//echo "Products Count: ". sizeof($listProducts);

		return $listProducts;
	}
	//===============================================================================

	private function makeTopProducts_SingleLang($catId=0) {
		$db = $this->_app->db();
		$listProducts = array();

		$q = "SELECT * FROM `products`";
		if($catId)
			$q .= " WHERE products_categories_id=".$catId;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		//echo $q;

		$res = $db->query($q);
		while($rPrd=@mysql_fetch_object($res)) {
			$prd = new VsProduct();

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->product_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;

			$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			$prd->_image = isset($rPrd->image) ? $rPrd->image : '';
			

			array_push($listProducts, $prd);
		}

		//echo "Products Count: ". sizeof($listProducts);

		return $listProducts;
	}
	//===============================================================================

	private function makeProduct_SingleLang($prdId) {
		$db = $this->_app->db();
		$prd = new VsProduct();

		$q = "SELECT * FROM `products` WHERE id=".$prdId;

		if($this->_pager)
			$q .= " LIMIT ".$this->_pager->from().", ".$this->_pager->view();

		$res = $db->query($q);
		if($res) {
			$rPrd=@mysql_fetch_object($res);

			$prd->_id = $rPrd->id;
			$prd->_categoryId = $rPrd->product_categories_id;
			$prd->_name = stripslashes($rPrd->name);
			$prd->_info = stripslashes($rPrd->info);
			$prd->_date = $rPrd->date;

			$prd->_price = isset($rPrd->price) ? $rPrd->price : 0.0;
			$prd->_image = isset($rPrd->image) ? $rPrd->image : '';
		}

		return $prd;
	}
	//===============================================================================


};

?>
